package com.jeesite.modules.kj.schedule.member;

import com.jeesite.common.config.Global;
import com.jeesite.modules.aliyun.msg.AliyunSmsUtils;
import com.jeesite.modules.kj.entity.KjMemberIntegrals;
import com.jeesite.modules.kj.entity.KjTiming;
import com.jeesite.modules.kj.service.KjMemberIntegralsService;
import com.jeesite.modules.kj.service.KjMemberService;
import com.jeesite.modules.kj.service.KjTimingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/*
* 定期推送信息给拥有积分和没有积分的用户
*
* */
@Component
@Configuration
@EnableScheduling
@Slf4j
public class MemberRemind {
    @Autowired
    KjMemberService kjMemberService;
    @Autowired
    KjMemberIntegralsService kjMemberIntegralsService;

    @Value("${msg.sms.template.pointsRemind}")
    private String pointsRemind;

    @Value("${msg.sms.template.noPointsRemind}")
    private String noPointsRemind;

    @Autowired
    KjTimingService kjTimingService;

    @Scheduled(cron = "10 0 10 0/1 * ?")
    //@Scheduled(cron = "0 0/1 * * * ?")
    public void fundMemberIntegrals(){
        int time= Integer.parseInt(Global.getConfig("sys.member.remind"))-1;
        Calendar cal=Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, -time);
        Date times=cal.getTime();
        KjTiming kjTiming=new KjTiming();
        kjTiming.setCreateDate_gte(times);
        List timing=kjTimingService.findList(kjTiming);
        if(timing.size()==0) {
            kjTimingService.save(kjTiming);
            List<String> has= kjMemberIntegralsService.getHasIntegralsMobile();
            List<String> no= kjMemberIntegralsService.getNoIntegralsMobile();
            for (String ha : has) {
                hasIntegral(ha);
            }
            for (String s : no) {
                notIntegral(s);
            }
        }
    }
    /*
    * 拥有积分
    * */
    public void hasIntegral(String mobile){
        AliyunSmsUtils.sendMessages(mobile,pointsRemind,new HashMap<>());
    }


    /*
    * 没有积分
    * */
    public void notIntegral(String mobile){
        AliyunSmsUtils.sendMessages(mobile,noPointsRemind,new HashMap<>());
    }
}
